package common;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JPanel;

/**
 * 能够拖拽图片的面板
 */
public class ImagePanel extends JPanel {
 
    private DragStatus status = DragStatus.Ready;   // 拖拽状态
 
    private Image image;                            // 要显示的图片
 
    private Point imagePosition = new Point(0, 0),  // 图片的当前位置
 
            imageStartposition = new Point(0, 0),   // 每次拖拽开始时图片的位置（也就是上次拖拽后的位置）
 
            mouseStartposition;                     // 每次拖拽开始时鼠标的位置
    public ImagePanel(){}
    public ImagePanel(Image image,Point nowPoint) {
    	this.image=image;
    	imagePosition=nowPoint;
        addMouseListener(new MouseListener() {
            // 双击鼠标时打开图片
            public void mouseClicked(MouseEvent e) {
            }
            // 按下鼠标时，更改状态，并且记录拖拽起始位置。
            public void mousePressed(MouseEvent e) {
                if (status == DragStatus.Ready) {
                    status = DragStatus.Dragging;
                    mouseStartposition = e.getPoint();
                    imageStartposition.setLocation(imagePosition.getLocation());
                }
            }
 
            // 松开鼠标时更改状态
            public void mouseReleased(MouseEvent e) {
                if (status == DragStatus.Dragging) {
                    status = DragStatus.Ready;
                }
            }
 
            public void mouseEntered(MouseEvent e) {}
            public void mouseExited(MouseEvent e) {}
        });
 
        addMouseMotionListener(new MouseMotionListener() {
            // Java 有拖拽事件，在这个事件中移动图片位置
            public void mouseDragged(MouseEvent e) {
                if (status == DragStatus.Dragging) {
                    moveImage(e.getPoint());
                }
            }
            public void mouseMoved(MouseEvent e) {
            }
        });
    }
 
    /**
     * 移动图片。实际上画图工作在 paintComponent() 中进行，这里只是计算图片位置，然后调用该方法。
     *
     * @param point 当前的鼠标位置
     */
    private void moveImage(Point point) {
        // 图片的当前位置等于图片的起始位置加上鼠标位置的偏移量。
        imagePosition.setLocation(
                imageStartposition.getX() + (point.getX() - mouseStartposition.getX()),
                imageStartposition.getY() + (point.getY() - mouseStartposition.getY())
        );
        repaint();
    }
 
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (image != null) {
            g.drawImage(image, (int) imagePosition.getX(), (int) imagePosition.getY(), this);
        }
    }
 
    public enum DragStatus {
        Ready, Dragging
    }

	public void setImage(Image image) {
		this.image = image;
	}
	public Point getImagePosition() {
		return imagePosition;
	}
	public void setImagePosition(Point imagePosition) {
		this.imagePosition = imagePosition;
	}
}